<?php

/**
 * static actions.
 *
 * @package    ndirou_affaire
 * @subpackage static
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class staticActions extends sfActions
{
	
 /**
  * Contact Us
  */	
  public function executeContactUs(sfWebRequest $request){
  	$this->form = new ContactUsForm();
  	if($request->isMethod('post') && $request->hasParameter('contactus')){
  		$data = $request->getParameter('contactus');
  		$captcha = array(
		  'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
		  'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
		);
		$this->form->bind(array_merge($data, array('captcha' => $captcha)));
        if( $this->form->isValid() ){
        	$this->dispatcher->notify(new sfEvent($this, 'seller.contactus',array( 
                    'name' => $this->form->getValue('name'), 
                    'url' => $this->form->getValue('url'),
        			'body' => $this->form->getValue('message'),
                    'from' => $this->form->getValue('email')  )));
                
            $this->getUser()->setFlash('success', 'Votre message a bien été envoyé, vous recevrez une réponse dans les prochains jours.');
            $this->redirect("@homepage");
        }
  	}
  }
	
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request){
    $this->newsletterform = new NewsletterAddForm();
    $this->newsletterform->setDefault('email', 'Votre E-Mail');
    $this->loginform = new LoginForm();
        
    $this->searchForm = new SearchAdsForm();
    $this->searchForm->setDefault('town_id',1);
    /* Login */
    if( $request->isMethod('post') && $request->hasParameter('login') ){
        $data = $request->getParameter('login');
        $this->loginform->bind($data);
        if( $this->loginform->isValid() ){
        	$this->getUser()->setAuthenticated(true);
        	$seller = Doctrine_Core::getTable("Seller")->findOneByEmailAndPassword($this->loginform->getValue('email'), $this->loginform->getValue('password'));
        	$this->getUser()->setAttribute('seller', $seller->getFullname() );
        	$this->getUser()->setAttribute('sellerId',$seller->id);
        }
    }
    /* Inscription newsletter */
    if( $request->isMethod('post') && $request->hasParameter('newsletter') ){
    	$this->newsletterform->bind( $request->getParameter('newsletter') );
    	if( $this->newsletterform->isValid() ){
    		$newsletter = $this->newsletterform->getObject();
    		$newsletter->email = $this->newsletterform->getValue('email');
    		$newsletter->status_id = Newsletter::_OPTIN;
    		$newsletter->save();
                
                $this->getUser()->setFlash('success', "Félicitation, désormais vous  faites partie de la communauté Bay3wachira");
    	}
    }
    /* Resultat de la recherche */
    if( $request->isMethod('post') && $request->hasParameter('search') ){
        $data = $request->getParameter('search');
        $this->searchForm->bind($data);
        if( $this->searchForm->isValid() ){
        	//echo "<pre>" . print_r($data,1) . "</pre>";
        	$this->setTemplate('search');
        	$this->aAds = Doctrine_core::getTable('Ads')->findAdsList($data);
        }
    }
  }
  
  /**
   * quiSommesNous
   */
  public function executeQuiSommesNous(sfWebRequest $request){
  	
  }
  /**
   * howsurfing
   */
  public function executeHowsurfing(sfWebRequest $request){
  	
  }
  
  
  
  
}
